/**
 * 初始商品列表信息
 * @param gooddata
 * @returns {Promise<void>}
 */
function initGoodsInfo(gooddata) {
    new Promise(function (resolve, reject) {
        layui.use("jquery", function () {
            var $ = layui.jquery;

            $.ajax({
                url: '/sys/getGoodsList',
                type: 'get',
                async: false,
                data: null,
                success: function (result) {
                    var goods = result.data;
                    goods.forEach(function (currval, index, arr) {
                        var goodinfo = {
                            goodCode: currval.goodCode,
                            goodName: currval.goodName
                        };
                        gooddata.push(goodinfo);
                    });
                    resolve(gooddata);
                },
                error: function () {
                    layer.msg('初始化商品数据失败,请联系管理员');
                }
            });
        });
    }).then(function (result) {
        return result;
    });
}

/**
 * 初始化商品来源
 * @param gooddata
 * @returns {Promise<void>}
 */
async function getGoodsSource(sourcedata) {
    var res = new Promise(function (resolve, reject) {
        layui.use("jquery", function () {
            var $ = layui.jquery;
            $.ajax({
                url: '/base/getSysCodeList',
                type: 'get',
                async: false,
                data: {"sysCode": '24'},
                success: function (result) {
                    var sources = result.data;
                    sources.forEach(function (currval, index, arr) {
                        var sourceinfo = {
                            source: currval.sysValue,
                            sourceName: currval.codeDesc
                        };
                        sourcedata.push(sourceinfo);
                    });
                    resolve(sourcedata);
                },
                error: function (error) {
                    layer.msg('初始化商品数据失败,请联系管理员');
                    reject(error);
                }
            });
        }).then(function (result) {
            console.log("加载商品信息完成", JSON.stringify(result));
            return result;
        });
    });
    return res;
}

/**
 * 初始商品列表信息
 * @param
 * @returns {Promise<void>}
 */
async function getGoodsInfo(){
    var goodData = await new Promise(function (resolve, reject) {
        layui.use("jquery", function () {
            var $ = layui.jquery;

            $.ajax({
                url: '/sys/getGoodsList',
                type: 'get',
                async: false,
                data: null,
                success: function (result) {
                    var goodArr = [];
                    var goods = result.data;
                    goods.forEach(function (currval, index, arr) {
                        var goodinfo = {
                            goodCode: currval.goodCode,
                            goodName: currval.goodName
                        };
                        goodArr.push(goodinfo);
                    });
                    resolve(goodArr);
                },
                error: function () {
                    layer.msg('初始化商品数据失败,请联系管理员');
                }
            });
        });
    }).then(function (result) {
        return result;
    });
    return goodData;
}

/**
 * 获取供货商信息
 * @param suppCode
 * @returns {Promise<*>}
 */
async function getSuppInfo(suppCode){
    if(typeof(suppCode) == 'undefined' || suppCode == null){
        console.log("请输入供货商代码");
        return null;
    }

    var res = await new Promise(function(resolve, reject){
        layui.use("jquery", function () {
            var $ = layui.jquery;

            $.ajax({
                url: '/base/getSuppInfo',
                type: 'GET',
                async: false,
                data: {suppCode: suppCode},
                success: function (result) {
                    var suppInfo = result.data;
                    resolve(suppInfo);
                },
                error: function () {
                    layer.msg('初始化商品数据失败,请联系管理员');
                }
            });
        });
    }).then((result) => {
        return result;
    });
    return res;
}